package lesson5;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;

public class MyControl extends JPanel {
    //这就是自定义控件
    //JPanel既是容器，也是控件


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        //绘制控件的样子
        //获取控件宽高
        int width=this.getWidth();
        int height=this.getHeight();
        //设置背景色
        g.setColor(Color.white);
        //设置形状
        g.fillRect(0,0,width,height);
        //绘制矩形
        g.setColor(Color.YELLOW);
        g.fillRect(0,0,100,100);
        //绘制椭圆
        g.setColor(Color.blue);
        g.fillOval(100,100,100,50);
        //绘制扇形
        g.setColor(Color.red);
        g.fillArc(200,200,50,50,0,120);
        //绘制图片
        //把图片加载进来,相对路径是从项目目录开始的
        File file=new File("data/study.jpg");
        //再读取图片
        try {
            BufferedImage imageStudy = ImageIO.read(file);
            //绘制图片
            g.drawImage(imageStudy,0,0,width,height,null);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
